package com.example.teesystem.controller;

import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.*;
import com.example.teesystem.entity.vo.chapter.*;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.ChapterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/tee/chapter", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class ChapterController {
    private final ChapterService chapterService;
    
    /**
     * 添加章节
     * @param chapterUploadVo 章节信息
     * @return
     */
    @PostMapping("/add")
    @PermissionCheck(PermissionUtil.COURSE_INFO_EDIT)
    public Response addChapter(@Valid @RequestBody(required = false) ChapterUploadVo chapterUploadVo) {
        if(chapterService.add(chapterUploadVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.ADD_INFO_FAIL);
        }
    }
    
//    /**
//     * 批量添加课章节，若有部分章节已经存在则不会添加已存在的章节
//     * @param chapterUploadVos 章节列表
//     * @return
//     */
//    @PostMapping("/addChapters")
//    public Response addChapters(@Valid @RequestBody(required = false) List<ChapterUploadVo> chapterUploadVos) {
//        for (ChapterUploadVo chapterUploadVo : chapterUploadVos) {
//            if (!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(), PermissionUtil.COURSE_INFO_EDIT, chapterUploadVo.getCourseId())) {
//                return Response.error(ResultCode.NO_PERMISSION);
//            }
//        }
//        if(chapterService.addChapters(chapterUploadVos)) {
//            return Response.ok();
//        } else {
//            throw new CustomException(ResultCode.ADD_INFO_FAIL);
//        }
//    }
    
    /**
     * 更新章节信息
     * @param chapterUpdateVo 章节信息
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck(PermissionUtil.COURSE_INFO_EDIT)
    public Response updateChapter(@Valid @RequestBody(required = false) ChapterUpdateVo chapterUpdateVo) {
        if(chapterService.update(chapterUpdateVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.UPDATE_INFO_FAIL);
        }
    }
    
    /**
     * 获取章节信息
     * @param chapterSelectVo 查询条件
     * @return
     */
    @PostMapping("/getChapters")
    public Response getChapters(@Valid @RequestBody ChapterSelectVo chapterSelectVo) {
        return Response.ok(chapterService.getChapters(chapterSelectVo));
    }
    
    /**
     * 章节删除
     * @param chapterDeleteVo 删除条件
     * @return
     */
    @PostMapping("/delete")
    @PermissionCheck(PermissionUtil.COURSE_INFO_EDIT)
    public Response delete(@Valid @RequestBody ChapterDeleteVo chapterDeleteVo) {
        if(chapterService.delete(chapterDeleteVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.DELETE_INFO_FAIL);
        }
    }
}
